package com.css.zfzx.fwsjcj.modules.structure.service.impl;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerDefaultImpl;
import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.PlatformApplication;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.org.dept.repository.entity.DeptEntity;
import com.css.bpm.platform.org.division.repository.DivisionRepository;
import com.css.bpm.platform.org.division.repository.entity.DivisionEntity;
import com.css.bpm.platform.org.role.repository.entity.RoleEntity;
import com.css.bpm.platform.org.user.repository.entity.UserEntity;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.bpm.platform.utils.PlatformSessionUtils;
import com.css.bpm.platform.utils.UUIDGenerator;
import com.css.zfzx.fwsjcj.commen.util.Constant;
import com.css.zfzx.fwsjcj.commen.util.EasyPoiExcelUtil;
import com.css.zfzx.fwsjcj.commen.util.ExcelStyleUtil;
import com.css.zfzx.fwsjcj.commen.util.ServerUtil;
import com.css.zfzx.fwsjcj.modules.analysis.vo.AnalysisQueryVo;
import com.css.zfzx.fwsjcj.modules.group.entity.FwGroupEntity;
import com.css.zfzx.fwsjcj.modules.group.repository.FwGroupRepository;
import com.css.zfzx.fwsjcj.modules.group.service.FwGroupService;
import com.css.zfzx.fwsjcj.modules.structure.entity.*;
import com.css.zfzx.fwsjcj.modules.structure.repository.*;
import com.css.zfzx.fwsjcj.modules.structure.service.*;
import com.css.zfzx.fwsjcj.modules.structure.vo.*;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

/**
 * @author ly
 * @Title: FwStructureFormServiceImpl
 * @Description: 房屋结构主表Service实现
 * @date 2020/6/2 15:32
 */
@Service
@PropertySource("classpath:platform-config.yml")
public class FwStructureFormServiceImpl<ststic> implements FwStructureFormService {
    // 主表service
    @Autowired
    private FwStructureFormRepository fwStructureFormRepository;
    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    @Autowired
    FwStructureFormQueryRepository fwStructureFormQueryRepository;

    // 超级管理员、省、市管理员编号
    @Value("${role.superCode}")
    private String superCode;
    @Value("${role.provinceCode}")
    private String provinceCode;
    @Value("${role.cityCode}")
    private String cityCode;
    @Value("${role.areaCode}")
    private String areaCode;//县管理员编号
    @Value("${role.xcCollectCode}")
    private String xcCollectCode;//详查采集员
    // base平台的地区接口
    @Autowired
    private DivisionRepository divisionRepository;
    @Autowired
    private FwStructureFormRepositoryQueryRepository fwStructureFormRepositoryQueryRepository;
    // 9种子表service
    @Autowired
    private FwBrickStructureService brickStructureService;
    @Autowired
    private FwBrickwoodStructureService brickwoodStructureService;
    @Autowired
    private FwBuildingStructureService buildingStructureService;
    @Autowired
    private FwFactoryStructureService factoryStructureService;
    @Autowired
    private FwHighStructureService highStructureService;
    @Autowired
    private FwMulConcreteStructureService mulConcreteStructureService;
    @Autowired
    private FwStoneStructureService stoneStructureService;
    @Autowired
    private FwWideStructureService wideStructureService;
    @Autowired
    private FwWoodStructureService woodStructureService;
    @Autowired
    private FwOtherStructureService otherStructureService;

    // 9个子表的Repository
    @Autowired
    private FwBrickStructureRepository brickStructureRepository;
    @Autowired
    private FwBrickwoodStructureRepository brickwoodStructureRepository;
    @Autowired
    private FwBuildingStructureRepository buildingStructureRepository;
    @Autowired
    private FwFactoryStructureRepository factoryStructureRepository;
    @Autowired
    private FwHighStructureRepository highStructureRepository;
    @Autowired
    private FwMulConcreteStructureRepository mulConcreteStructureRepository;
    @Autowired
    private FwStoneStructureRepository stoneStructureRepository;
    @Autowired
    private FwWideStructureRepository wideStructureRepository;
    @Autowired
    private FwWoodStructureRepository woodStructureRepository;
    @Autowired
    private FwOtherStructureRepository otherStructureRepository;
    @Autowired
    FwGroupRepository fwGroupRepository;

    @Override
    public void save(FwStructureFormEntity fwStructureFormEntity) {
        String userId = PlatformSessionUtils.getUserId();
        UserEntity user = PlatformAPI.getOrgAPI().getUserAPI().getUser(userId);
        /**
         * 设置默认值
         */
        fwStructureFormEntity.setId(UUIDGenerator.getUUID());
        // 数据收集人名称
        fwStructureFormEntity.setCollectUser(user.getUserName());
        // 存数据收集人ID
        fwStructureFormEntity.setCreateUser(userId);
        fwStructureFormEntity.setCreateTime(new Date());

        fwStructureFormEntity.setDelFlag(Constant.DEL_NO);

        fwStructureFormRepository.save(fwStructureFormEntity);
    }

    @Override
    public void update(FwStructureFormEntity fwStructureFormEntity) {
        if (StringUtils.isEmpty(fwStructureFormEntity.getId())) {
            throw new RuntimeException("修改ID不能为空");
        }
        // 获取当前用户信息
        String userId = PlatformSessionUtils.getUserId();
        UserEntity user = PlatformAPI.getOrgAPI().getUserAPI().getUser(userId);
        /**
         * 设置默认值
         */
        fwStructureFormEntity.setUpdateUser(user.getUserId());
        fwStructureFormEntity.setUpdateTime(new Date());

        fwStructureFormRepository.save(fwStructureFormEntity);
    }

    @Override
    public JSONObject getPage(StructureQueryVo structureQueryVo, int curPage, int pageSize) {
        Page<FwStructureFormEntity> structFormPage = fwStructureFormRepositoryQueryRepository.getSructFormPage(structureQueryVo, curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(structFormPage);
        return jsonObject;
    }

    private String getRoleByUserId(String userId) {
        // 获取当前登录用户角色
        List<RoleEntity> roles = PlatformAPI.getOrgAPI().getUserAPI().getRoles(userId);
        // 角色列表里面是否包含三个管理员
        //是否为超级管理员
        if (ServerUtil.isContaintRole(roles, superCode)){
            return "1";
        }
        //是否为省级管理员
        if (ServerUtil.isContaintRole(roles, provinceCode)){
            return "2";
        }
        //是否为市级管理员
        if (ServerUtil.isContaintRole(roles, cityCode)){
            return "3";
        }
        //是否为县级管理员
        if (ServerUtil.isContaintRole(roles, areaCode)){
            return "4";
        }
        //是否为详查采集员
        /*if (ServerUtil.isContaintRole(roles, xcCollectCode)){
            return "5";
        }*/
        //是否是组长
        List<FwGroupEntity> byGroupLeaderId = fwGroupRepository.findAllByGroupLeaderId(userId);
        if (byGroupLeaderId != null && byGroupLeaderId.size() > 0){
            return "6";
        }
        return "";
    }

    @Override
    public JSONObject getCheckPage(StructureQueryVo structureQueryVo, int curPage, int pageSize) {
        String userId = PlatformSessionUtils.getUserId();
        String role = getRoleByUserId(userId);
        Page<FwStructureFormEntity> structFormPage =fwStructureFormRepositoryQueryRepository.getCheckPage(role,structureQueryVo,curPage,pageSize);

       /* QFwStructureFormEntity q = QFwStructureFormEntity.fwStructureFormEntity;
        Predicate predicate = q.isNotNull().or(q.isNull());

        // 查询条件
        if (structureQueryVo != null) {
            String taskId = structureQueryVo.getTaskId();
            predicate = ("".equals(taskId) || taskId == null) ? predicate : ExpressionUtils.and(predicate, q.taskId.eq(taskId));

            // 任务名称
            String taskName = structureQueryVo.getTaskName();
            predicate = ("".equals(taskName) || taskName == null) ? predicate : ExpressionUtils.and(predicate, q.taskName.like("%" + taskName + "%"));

            // 建筑名称
            String buildingName = structureQueryVo.getBuildingName();
            predicate = ("".equals(buildingName) || buildingName == null) ? predicate : ExpressionUtils.and(predicate, q.buildingName.like("%" + buildingName + "%"));

            String belongtoProvince = structureQueryVo.getBelongtoProvince();
            predicate = ("".equals(belongtoProvince) || belongtoProvince == null) ? predicate : ExpressionUtils.and(predicate, q.belongtoProvince.like("%" + belongtoProvince + "%"));

            String belongtoCity = structureQueryVo.getBelongtoCity();
            predicate = ("".equals(belongtoCity) || belongtoCity == null) ? predicate : ExpressionUtils.and(predicate, q.belongtoCity.like("%" + belongtoCity + "%"));

            // 建筑类别（8种子表类型）
            String structureType = structureQueryVo.getStructureType();
            predicate = ("".equals(structureType) || structureType == null) ? predicate : ExpressionUtils.and(predicate, q.structureType.like("%" + structureType + "%"));

            // 审核状态
            String status = structureQueryVo.getStatus();
            predicate = ("".equals(status) || status == null) ? predicate : ExpressionUtils.and(predicate, q.status.eq(status));
        }

        *//**
         * 登录用户查询权限限制
         *//*
        String userId = PlatformSessionUtils.getUserId();
        // 获取当前登录用户角色
        List<RoleEntity> roles = PlatformAPI.getOrgAPI().getUserAPI().getRoles(userId);
        // 角色列表里面是否包含三个管理员
        boolean containtSuperRole = ServerUtil.isContaintRole(roles, superCode);
        boolean containtProRole = ServerUtil.isContaintRole(roles, provinceCode);
        boolean containtCityRole = ServerUtil.isContaintRole(roles, cityCode);
        // 若都不包含
        if (!containtSuperRole) {
            if (!containtProRole && !containtCityRole) {
                // 查询本人创建的
                predicate = ExpressionUtils.and(predicate, q.createUser.eq(userId));
            } else {
                // 获取当前用户的行政区域
                DeptEntity mainDept = PlatformAPI.getOrgAPI().getUserAPI().getMainDept(userId);
                String divisionId = mainDept.getDivisionId();
                DivisionEntity validDivisionById = divisionRepository.findValidDivisionById(divisionId);
                String divisionName = validDivisionById.getDivisionName();
                // 省级管理员（超级管理员不需要筛选）
                if (containtProRole) {
                    predicate = ExpressionUtils.and(predicate, q.belongtoProvince.contains(divisionName));
                }
                // 市级管理员
                if (containtCityRole) {
                    predicate = ExpressionUtils.and(predicate, q.belongtoCity.contains(divisionName));
                }
            }
        }

        // 删除标识
        predicate = ExpressionUtils.and(predicate, q.delFlag.eq(Constant.DEL_NO));
        // 审核状态不为未提交的
        predicate = ExpressionUtils.and(predicate, q.status.ne(Constant.NO_SUB));

        QueryResults<FwStructureFormEntity> page = jpaQueryFactory
                .selectFrom(q)
                .where(predicate)
                .orderBy(q.createTime.desc())
                .offset(curPage - 1)   //起始页
                .limit(pageSize)  //每页大小
                .fetchResults();
        *//**
         * 适配base转为JSONObject类型
         *//*
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rows", page.getResults());
        jsonObject.put("total", page.getTotal());*/
        JSONObject jsonObject = PlatformPageUtils.formatPageData(structFormPage);
        return jsonObject;
    }

    @Override
    @Transactional
    public void delete(List<String> idList) {
        for (String id : idList) {
            FwStructureFormEntity fwStructureFormEntity = fwStructureFormRepository.findById(id).get();
            fwStructureFormEntity.setDelFlag(Constant.DEL_YES);
            update(fwStructureFormEntity);

            String structureId = fwStructureFormEntity.getStructureId();
            String structureType = fwStructureFormEntity.getStructureType();
            // 修改子表
            if (Constant.BRICK_STRUCTURE.equals(structureType)) {
                // 砖混结构子表
                brickStructureService.delete(structureId);
            }
            if (Constant.BUILDING_STRUCTURE.equals(structureType)) {
                // 土木结构子表
                buildingStructureService.delete(structureId);
            }
            if (Constant.FACTORY_STRUCTURE.equals(structureType)) {
                // 工业厂房结构子表
                factoryStructureService.delete(structureId);
            }
            if (Constant.HIGH_STRUCTURE.equals(structureType)) {
                // 高层结构
                highStructureService.delete(structureId);
            }
            if (Constant.MUL_CONCRETE_STRUCTURE.equals(structureType)) {
                // 多层钢筋混凝土
                mulConcreteStructureService.delete(structureId);
            }
            if (Constant.STONE_STRUCTURE.equals(structureType)) {
                // 石结构
                stoneStructureService.delete(structureId);
            }
            if (Constant.WIDE_STRUCTURE.equals(structureType)) {
                // 空旷结构
                wideStructureService.delete(structureId);
            }
            if (Constant.WOOD_STRUCTURE.equals(structureType)) {
                // 木结构
                woodStructureService.delete(structureId);
            }
            if (Constant.OTHER_STRUCTURE.equals(structureType)) {
                // 其它
                otherStructureService.delete(structureId);
            }
            if (Constant.BRICKWOOD_STRUCTURE.equals(structureType)) {
                // 砖木结构
                brickwoodStructureService.delete(structureId);
            }
        }
    }


    @Override
    public FwStructureFormEntity findById(String id) {
        return fwStructureFormRepository.findFwStructureFormEntityByIdAndDelFlag(id, Constant.DEL_NO);
    }

    @Override
    public FwStructureFormEntity findStructureForm(String type, String id) {
        QFwStructureFormEntity q = QFwStructureFormEntity.fwStructureFormEntity;
        return jpaQueryFactory.selectFrom(q).where(q.structureType.eq(type), q.structureId.eq(id)).fetchOne();
    }

    @Override
    @Transactional
    public void check(String id, CheckVo checkVo) {
        // 获取当前用户信息
        String userId = PlatformSessionUtils.getUserId();
        UserEntity user = PlatformAPI.getOrgAPI().getUserAPI().getUser(userId);

        FwStructureFormEntity structureFormEntity = findById(id);
        // 主表设置审核状态与原因
        structureFormEntity.setStatus(checkVo.getStatus());
        structureFormEntity.setCheckUser(user.getUserName());
        structureFormEntity.setCheckTime(new Date());
        structureFormEntity.setCheckOpinion(checkVo.getCheckOpinion());
        this.update(structureFormEntity);

        // 根据类型判断出具体子表，根据子表Id修改子表审核信息
        String structureId = structureFormEntity.getStructureId();
        String structureType = structureFormEntity.getStructureType();
        // 修改子表
        if (Constant.BRICK_STRUCTURE.equals(structureType)) {
            // 砖混结构子表
            FwBrickStructureEntity brickStructureEntity = brickStructureService.findById(structureId);
            brickStructureEntity.setStatus(checkVo.getStatus());
            brickStructureEntity.setCheckUser(user.getUserName());
            brickStructureEntity.setCheckOpinion(checkVo.getCheckOpinion());
            brickStructureRepository.save(brickStructureEntity);
        }
        if (Constant.BUILDING_STRUCTURE.equals(structureType)) {
            // 土木结构子表
            FwBuildingStructureEntity buildingStructureEntity = buildingStructureService.findById(structureId);
            buildingStructureEntity.setStatus(checkVo.getStatus());
            buildingStructureEntity.setCheckUser(user.getUserName());
            buildingStructureEntity.setCheckOpinion(checkVo.getCheckOpinion());
            buildingStructureRepository.save(buildingStructureEntity);
        }
        if (Constant.FACTORY_STRUCTURE.equals(structureType)) {
            // 工业厂房结构子表
            FwFactoryStructureEntity factoryStructureEntity = factoryStructureService.findById(structureId);
            factoryStructureEntity.setStatus(checkVo.getStatus());
            factoryStructureEntity.setCheckUser(user.getUserName());
            factoryStructureEntity.setCheckOpinion(checkVo.getCheckOpinion());
            factoryStructureRepository.save(factoryStructureEntity);
        }
        if (Constant.HIGH_STRUCTURE.equals(structureType)) {
            // 高层结构
            FwHighStructureEntity highStructureEntity = highStructureService.findById(structureId);
            highStructureEntity.setStatus(checkVo.getStatus());
            highStructureEntity.setCheckUser(user.getUserName());
            highStructureEntity.setCheckOpinion(checkVo.getCheckOpinion());
            highStructureRepository.save(highStructureEntity);
        }
        if (Constant.MUL_CONCRETE_STRUCTURE.equals(structureType)) {
            // 多层钢筋混凝土
            FwMulConcreteStructureEntity mulConcreteStructureEntity = mulConcreteStructureService.findById(structureId);
            mulConcreteStructureEntity.setStatus(checkVo.getStatus());
            mulConcreteStructureEntity.setCheckUser(user.getUserName());
            mulConcreteStructureEntity.setCheckOpinion(checkVo.getCheckOpinion());
            mulConcreteStructureRepository.save(mulConcreteStructureEntity);
        }
        if (Constant.STONE_STRUCTURE.equals(structureType)) {
            // 石结构
            FwStoneStructureEntity stoneStructureEntity = stoneStructureService.findById(structureId);
            stoneStructureEntity.setStatus(checkVo.getStatus());
            stoneStructureEntity.setCheckUser(user.getUserName());
            stoneStructureEntity.setCheckOpinion(checkVo.getCheckOpinion());
            stoneStructureRepository.save(stoneStructureEntity);
        }
        if (Constant.WIDE_STRUCTURE.equals(structureType)) {
            // 空旷结构
            FwWideStructureEntity wideStructureEntity = wideStructureService.findById(structureId);
            wideStructureEntity.setStatus(checkVo.getStatus());
            wideStructureEntity.setCheckUser(user.getUserName());
            wideStructureEntity.setCheckOpinion(checkVo.getCheckOpinion());
            wideStructureRepository.save(wideStructureEntity);
        }
        if (Constant.WOOD_STRUCTURE.equals(structureType)) {
            // 木结构
            FwWoodStructureEntity woodStructureEntity = woodStructureService.findById(structureId);
            woodStructureEntity.setStatus(checkVo.getStatus());
            woodStructureEntity.setCheckUser(user.getUserName());
            woodStructureEntity.setCheckOpinion(checkVo.getCheckOpinion());
            woodStructureRepository.save(woodStructureEntity);
        }
        if (Constant.OTHER_STRUCTURE.equals(structureType)) {
            // 其它结构
            FwOtherStructureEntity otherStructureEntity = otherStructureService.findById(structureId);
            otherStructureEntity.setStatus(checkVo.getStatus());
            otherStructureEntity.setCheckUser(user.getUserName());
            otherStructureEntity.setCheckOpinion(checkVo.getCheckOpinion());
            otherStructureRepository.save(otherStructureEntity);
        }
    }

    @Override
    @Transactional
    public void check(CheckVo checkVo) {
        String ids = checkVo.getId();
        List<String> idList = Arrays.asList(ids.split(","));
        for (String id : idList) {
            check(id, checkVo);
        }
    }

    @Override
    @Transactional
    public void submit(List<String> idList) {
        QFwStructureFormEntity q = QFwStructureFormEntity.fwStructureFormEntity;
        jpaQueryFactory.update(q)
                .set(q.status, Constant.DAI_SP)
                .set(q.submitTime, new Date())
                .where(q.id.in(idList))
                .execute();
        for (String id : idList) {
            FwStructureFormEntity structureFormEntity = findById(id);
            //子表结构类型
            String structureType = structureFormEntity.getStructureType();
            // 子表结构ID
            String structureId = structureFormEntity.getStructureId();
            ArrayList<String> list = new ArrayList<>();
            list.add(structureId);
            // 修改子表
            if (Constant.BRICK_STRUCTURE.equals(structureType)) {
                // 砖混结构子表
                brickStructureService.submit(list);
            }
            if (Constant.BUILDING_STRUCTURE.equals(structureType)) {
                // 土木结构子表
                buildingStructureService.submit(list);
            }
            if (Constant.FACTORY_STRUCTURE.equals(structureType)) {
                // 工业厂房结构子表
                factoryStructureService.submit(list);
            }
            if (Constant.HIGH_STRUCTURE.equals(structureType)) {
                // 高层结构
                highStructureService.submit(list);
            }
            if (Constant.MUL_CONCRETE_STRUCTURE.equals(structureType)) {
                // 多层钢筋混凝土
                mulConcreteStructureService.submit(list);
            }
            if (Constant.STONE_STRUCTURE.equals(structureType)) {
                // 石结构
                stoneStructureService.submit(list);
            }
            if (Constant.WIDE_STRUCTURE.equals(structureType)) {
                // 空旷结构
                wideStructureService.submit(list);
            }
            if (Constant.WOOD_STRUCTURE.equals(structureType)) {
                // 木结构
                woodStructureService.submit(list);
            }
            if (Constant.OTHER_STRUCTURE.equals(structureType)) {
                // 其它结构
                otherStructureService.submit(list);
            }
        }
    }

    @Override
    public Object getChild(String id) {
        Object object = new Object();
        FwStructureFormEntity fwStructureFormEntity = fwStructureFormRepository.findById(id).get();
        String structureId = fwStructureFormEntity.getStructureId();
        String structureType = fwStructureFormEntity.getStructureType();
        // 修改子表
        if (Constant.BRICK_STRUCTURE.equals(structureType)) {
            // 砖混结构子表
            object = brickStructureService.findById(structureId);
        }
        if (Constant.BUILDING_STRUCTURE.equals(structureType)) {
            // 土木结构子表
            object = buildingStructureService.findById(structureId);
        }
        if (Constant.FACTORY_STRUCTURE.equals(structureType)) {
            // 工业厂房结构子表
            object = factoryStructureService.findById(structureId);
        }
        if (Constant.HIGH_STRUCTURE.equals(structureType)) {
            // 高层结构
            object = highStructureService.findById(structureId);
        }
        if (Constant.MUL_CONCRETE_STRUCTURE.equals(structureType)) {
            // 多层钢筋混凝土
            object = mulConcreteStructureService.findById(structureId);
        }
        if (Constant.STONE_STRUCTURE.equals(structureType)) {
            // 石结构
            object = stoneStructureService.findById(structureId);
        }
        if (Constant.WIDE_STRUCTURE.equals(structureType)) {
            // 空旷结构
            object = wideStructureService.findById(structureId);
        }
        if (Constant.WOOD_STRUCTURE.equals(structureType)) {
            // 木结构
            object = woodStructureService.findById(structureId);
        }
        if (Constant.OTHER_STRUCTURE.equals(structureType)) {
            // 其它结构
            object = otherStructureService.findById(structureId);
        }
        if (Constant.BRICKWOOD_STRUCTURE.equals(structureType)) {
            // 砖木结构
            object = brickwoodStructureService.findById(structureId);
        }
        return object;
    }

    @Override
    public List<String> getBuildingNameList(AnalysisQueryVo analysisQueryVo) {
        // 查询条件
        Specification querySpecification = new Specification<FwStructureFormEntity>() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root<FwStructureFormEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>();
                if (analysisQueryVo != null) {
                    // 省
                    String belongtoProvince = analysisQueryVo.getBelongtoProvince();
                    if (!org.apache.commons.lang3.StringUtils.isEmpty(belongtoProvince)) {
                        predicates.add(criteriaBuilder.equal(root.get("belongtoProvince"), belongtoProvince));
                    }
                    // 市
                    String belongtoCity = analysisQueryVo.getBelongtoCity();
                    if (!org.apache.commons.lang3.StringUtils.isEmpty(belongtoCity)) {
                        predicates.add(criteriaBuilder.equal(root.get("belongtoCity"), belongtoCity));
                    }
                    // 区
                    String belongtoArea = analysisQueryVo.getBelongtoArea();
                    if (!StringUtils.isEmpty(belongtoArea)) {
                        predicates.add(criteriaBuilder.equal(root.get("belongtoArea"), belongtoArea));
                    }
                    // 审批类型
                    String spType = analysisQueryVo.getSpType();
                    if (!StringUtils.isEmpty(spType)) {
                        if ("审核通过".equals(spType)) {
                            predicates.add(criteriaBuilder.equal(root.get("status"), Constant.SP_YES));
                        } else {
                            predicates.add(criteriaBuilder.equal(root.get("status"), Constant.SP_YES));
                        }
                    }
                    // 结构类型
                    String structureType = analysisQueryVo.getStructureType();
                    if (!org.apache.commons.lang3.StringUtils.isEmpty(structureType)) {
                        predicates.add(criteriaBuilder.equal(root.get("structureType"), structureType));
                    }
                }
                // 有效标识，即为有效的
                predicates.add(criteriaBuilder.equal(root.get("delFlag"), Constant.DEL_NO));
                return criteriaBuilder.and(predicates.toArray(new javax.persistence.criteria.Predicate[predicates.size()]));
            }
        };
        List<FwStructureFormEntity> all = fwStructureFormRepository.findAll(querySpecification);
        // 遍历取buildingName值放入数组中
        ArrayList<String> buildingNames = new ArrayList<>();
        for (FwStructureFormEntity fwStructureFormEntity : all) {
            buildingNames.add(fwStructureFormEntity.getBuildingName());
        }
        return buildingNames;
    }

    @Override
    public List<FwStructureFormEntity> getFormList(String provice, String city,String area,String taskName, String spType) {
        // 查询条件
        Specification querySpecification = new Specification<FwStructureFormEntity>() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root<FwStructureFormEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>();
                // 省
                if (!org.apache.commons.lang3.StringUtils.isEmpty(provice)) {
                    predicates.add(criteriaBuilder.equal(root.get("belongtoProvince"), provice));
                }
                // 市
                if (!org.apache.commons.lang3.StringUtils.isEmpty(city)) {
                    predicates.add(criteriaBuilder.equal(root.get("belongtoCity"), city));
                }
                // 区县
                if (!org.apache.commons.lang3.StringUtils.isEmpty(area)) {
                    predicates.add(criteriaBuilder.equal(root.get("belongtoArea"), area));
                }

                //任务名称
                if (!org.apache.commons.lang3.StringUtils.isEmpty(taskName)) {
                    String[] strArray = taskName.split(",");
                    List<String> list = Arrays.asList(strArray);
                    if (list != null && list.size() > 0){
                        predicates.add(root.get("taskName").in(list));
                    }
                }
                /*
                // 任务名称
                if (!org.apache.commons.lang3.StringUtils.isEmpty(taskName)) {
                    predicates.add(criteriaBuilder.equal(root.get("taskName"), taskName));
                }*/

                // 审批类型
                if (!org.apache.commons.lang3.StringUtils.isEmpty(spType)) {
                    predicates.add(criteriaBuilder.equal(root.get("status"), spType));
                }
                // 有效标识，即为有效的
                predicates.add(criteriaBuilder.equal(root.get("delFlag"), Constant.DEL_NO));
                return criteriaBuilder.and(predicates.toArray(new javax.persistence.criteria.Predicate[predicates.size()]));
            }
        };
        List<FwStructureFormEntity> all = fwStructureFormRepository.findAll(querySpecification);
        return all;
    }

    @Transactional
    @Override
    public String updateFile(String formId, String type, String fileId) {
        if (!StringUtils.isEmpty(fileId) && !StringUtils.isEmpty(type) && !StringUtils.isEmpty(formId)) {
            if (Constant.BRICK_STRUCTURE.equals(type)) {
                brickStructureService.updateFileId(formId, fileId);
            }
            if (Constant.BUILDING_STRUCTURE.equals(type)) {
                buildingStructureService.updateFileId(formId, fileId);
            }
            if (Constant.FACTORY_STRUCTURE.equals(type)) {
                factoryStructureService.updateFileId(formId, fileId);
            }
            if (Constant.HIGH_STRUCTURE.equals(type)) {
                highStructureService.updateFileId(formId, fileId);
            }
            if (Constant.MUL_CONCRETE_STRUCTURE.equals(type)) {
                mulConcreteStructureService.updateFileId(formId, fileId);
            }
            if (Constant.STONE_STRUCTURE.equals(type)) {
                stoneStructureService.updateFileId(formId, fileId);
            }
            if (Constant.WIDE_STRUCTURE.equals(type)) {
                wideStructureService.updateFileId(formId, fileId);
            }
            if (Constant.WOOD_STRUCTURE.equals(type)) {
                woodStructureService.updateFileId(formId, fileId);
            }
            if (Constant.OTHER_STRUCTURE.equals(type)) {
                otherStructureService.updateFileId(formId, fileId);
            }
            if (Constant.BRICKWOOD_STRUCTURE.equals(type)) {
                brickwoodStructureService.updateFileId(formId, fileId);
            }
            return "上传成功";
        } else {
            return "上传失败";
        }
    }

    @Override
    public List<String> getIdsByStructureType(StructureQueryVo structureQueryVo) {

        List<String> ids =fwStructureFormRepositoryQueryRepository.getIds(structureQueryVo);
        /*if(structureQueryVo.getStatus().equals("null")){
            structureQueryVo.setStatus("");
        }
        QFwStructureFormEntity q = QFwStructureFormEntity.fwStructureFormEntity;
        Predicate predicate = q.isNotNull().or(q.isNull());

        // 查询条件:审核状态
        ArrayList<String> status = new ArrayList<>();
        status.add(Constant.SP_NOT);
        status.add(Constant.SP_YES);
        status.add(Constant.DAI_SP);
        status.add(Constant.SJ_TH);
        status.add(Constant.ZX_TH);
        predicate = ExpressionUtils.and(predicate, q.status.in(status));
        // 详查子表类型
        if(!StringUtils.isEmpty(structureQueryVo.getStatus())){
            predicate = ExpressionUtils.and(predicate, q.status.eq(structureQueryVo.getStatus()));
        }
        predicate = ExpressionUtils.and(predicate, q.structureType.in(structureQueryVo.getStructureType()));
        // 删除标识
        predicate = ExpressionUtils.and(predicate, q.delFlag.eq(Constant.DEL_NO));
        if(!StringUtils.isEmpty(structureQueryVo.getBelongtoProvince())){
            predicate = ExpressionUtils.and(predicate, q.belongtoProvince.eq(structureQueryVo.getBelongtoProvince()));
        }
        if(!StringUtils.isEmpty(structureQueryVo.getBelongtoCity())){
            predicate = ExpressionUtils.and(predicate, q.belongtoCity.eq(structureQueryVo.getBelongtoCity()));
        }
        if(!StringUtils.isEmpty(structureQueryVo.getBelongtoArea())){
            predicate = ExpressionUtils.and(predicate, q.belongtoArea.eq(structureQueryVo.getBelongtoArea()));
        }
        if(!StringUtils.isEmpty(structureQueryVo.getStructureNum())){
            predicate = ExpressionUtils.and(predicate, q.structureNum.like("%"+structureQueryVo.getStructureNum()+"%"));
        }
        if(!StringUtils.isEmpty(structureQueryVo.getBuildingName())){
            predicate = ExpressionUtils.and(predicate, q.buildingName.like("%"+structureQueryVo.getBuildingName()+"%"));
        }
        if(!StringUtils.isEmpty(structureQueryVo.getCollectUser())){
            predicate = ExpressionUtils.and(predicate, q.collectUser.like("%"+structureQueryVo.getCollectUser()+"%"));
        }
        if(!StringUtils.isEmpty(structureQueryVo.getTaskName())){
            predicate = ExpressionUtils.and(predicate, q.taskName.like("%"+structureQueryVo.getTaskName()+"%"));
        }

        *//**
         * 登录用户查询权限限制
         *//*
        String userId = PlatformSessionUtils.getUserId();
        // 获取当前登录用户角色
        List<RoleEntity> roles = PlatformAPI.getOrgAPI().getUserAPI().getRoles(userId);
        // 角色列表里面是否包含三个管理员
        boolean containtSuperRole = ServerUtil.isContaintRole(roles, superCode);
        boolean containtProRole = ServerUtil.isContaintRole(roles, provinceCode);
        boolean containtCityRole = ServerUtil.isContaintRole(roles, cityCode);
        // 若都不包含
        if (!containtSuperRole) {
            if (!containtProRole && !containtCityRole) {
                // 查询本人创建的
                predicate = ExpressionUtils.and(predicate, q.createUser.eq(userId));
            } else {
                // 获取当前用户的行政区域
                DeptEntity mainDept = PlatformAPI.getOrgAPI().getUserAPI().getMainDept(userId);
                String divisionId = mainDept.getDivisionId();
                DivisionEntity validDivisionById = divisionRepository.findValidDivisionById(divisionId);
                String divisionName = validDivisionById.getDivisionName();
                // 省级管理员（超级管理员不需要筛选）
                if (containtProRole) {
                    predicate = ExpressionUtils.and(predicate, q.belongtoProvince.contains(divisionName));
                }
                // 市级管理员
                if (containtCityRole) {
                    predicate = ExpressionUtils.and(predicate, q.belongtoCity.contains(divisionName));
                }
            }
        }
        List<String> list = jpaQueryFactory.select(q.structureId)
                .from(q)
                .where(predicate)
                .orderBy(q.createTime.desc())
                .fetch();*/
        return ids;
    }

    @Override
    public Boolean isEmpty(StructureQueryVo structureQueryVo) {
        List<String> list = this.getIdsByStructureType(structureQueryVo);
        if (list == null || list.size() <= 0) {
            return true;
        }
        return false;
    }

    @Override
    public void export(StructureQueryVo structureQueryVo, HttpServletResponse response) {
        if(structureQueryVo.getStatus().equals("null")){
            structureQueryVo.setStatus("");
        }
        if(structureQueryVo.getCheckStatus().equals("null")){
            structureQueryVo.setCheckStatus("");
        }
        List<String> list = this.getIdsByStructureType(structureQueryVo);
        if (Constant.BRICK_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            brickStructureService.export(list, response);
        }
        if (Constant.MUL_CONCRETE_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            mulConcreteStructureService.export(list, response);
        }
        if (Constant.HIGH_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            highStructureService.export(list, response);
        }
        if (Constant.FACTORY_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            factoryStructureService.export(list, response);
        }
        if (Constant.WIDE_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            wideStructureService.export(list, response);
        }
        if (Constant.BUILDING_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            buildingStructureService.export(list, response);
        }
        if (Constant.WOOD_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            woodStructureService.export(list, response);
        }
        if (Constant.STONE_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            stoneStructureService.export(list, response);
        }
        if (Constant.OTHER_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            otherStructureService.export(list, response);
        }
        if (Constant.BRICKWOOD_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            brickwoodStructureService.export(list, response);
        }
    }

    /**
     * 按查询条件统计栋数
     *
     * @param analysisQueryVo
     * @return
     */
    @Override
    public Long findCount(AnalysisQueryVo analysisQueryVo) {
        //砖混结构
        long brickCount=brickStructureService.count(analysisQueryVo);
        //砖木结构
        long brickwoodCount=brickwoodStructureService.count(analysisQueryVo);
        //土木结构
        long buildCount= buildingStructureService.count(analysisQueryVo);
        //工业厂房结构
        long factoryCount = factoryStructureService.count(analysisQueryVo);
        //高层结构
        long highCount = highStructureService.count(analysisQueryVo);
        //多层钢筋混凝土结构
        long mulConcreteCount = mulConcreteStructureService.count(analysisQueryVo);
        //其它结构
        long otherCount = otherStructureService.count(analysisQueryVo);
        //石结构
        long stoneCount = stoneStructureService.count(analysisQueryVo);
        //空旷结构
        long wideCount = wideStructureService.count(analysisQueryVo);
        //木结构
        long woodCount = woodStructureService.count(analysisQueryVo);

        long allCount = brickCount + brickwoodCount + buildCount + factoryCount + highCount + mulConcreteCount
                + otherCount + stoneCount + wideCount +woodCount;
        return allCount;
    }

    /**
     * 按查询条件统计面积
     *
     * @param analysisQueryVo
     * @return
     */
    @Override
    public Double findAreaCount(AnalysisQueryVo analysisQueryVo) {
        //砖混结构
        Double brickCount=brickStructureService.area(analysisQueryVo);
        //砖木结构
        Double brickwoodCount=brickwoodStructureService.area(analysisQueryVo);
        //土木结构
        Double buildCount= buildingStructureService.area(analysisQueryVo);
        //工业厂房结构
        Double factoryCount = factoryStructureService.area(analysisQueryVo);
        //高层结构
        Double highCount = highStructureService.area(analysisQueryVo);
        //多层钢筋混凝土结构
        Double mulConcreteCount = mulConcreteStructureService.area(analysisQueryVo);
        //其它结构
        Double otherCount = otherStructureService.area(analysisQueryVo);
        //石结构
        Double stoneCount = stoneStructureService.area(analysisQueryVo);
        //空旷结构
        Double wideCount = wideStructureService.area(analysisQueryVo);
        //木结构
        Double woodCount = woodStructureService.area(analysisQueryVo);
        Double allAreaCount = brickCount + brickwoodCount + buildCount + factoryCount + highCount + mulConcreteCount
                + otherCount + stoneCount + wideCount +woodCount;
        return allAreaCount;
    }


    /**
     * 按类型结构统计栋数
     *
     * @param analysisQueryVo
     * @return
     */
    @Override
    public Map<String, Object> findbuildCountMap(AnalysisQueryVo analysisQueryVo) {
        //砖混结构
        long brickCount=brickStructureService.count(analysisQueryVo);
        //砖木结构
        long brickwoodCount=brickwoodStructureService.count(analysisQueryVo);
        //土木结构
        long buildCount= buildingStructureService.count(analysisQueryVo);
        //工业厂房结构
        long factoryCount = factoryStructureService.count(analysisQueryVo);
        //高层结构
        long highCount = highStructureService.count(analysisQueryVo);
        //多层钢筋混凝土结构
        long mulConcreteCount = mulConcreteStructureService.count(analysisQueryVo);
        //其它结构
        long otherCount = otherStructureService.count(analysisQueryVo);
        //石结构
        long stoneCount = stoneStructureService.count(analysisQueryVo);
        //空旷结构
        long wideCount = wideStructureService.count(analysisQueryVo);
        //木结构
        long woodCount = woodStructureService.count(analysisQueryVo);
        // X轴数据
        ArrayList<String> x = new ArrayList<>();
        x.add("砖混结构");
        x.add("砖木结构");
        x.add("土木结构");
        x.add("工业厂房结构");
        x.add("高层结构");
        x.add("多层钢筋混凝土结构");
        x.add("石结构");
        x.add("空旷结构");
        x.add("木结构");
        x.add("其它结构");
        // Y轴数据
        ArrayList<Long> y = new ArrayList<>();
        y.add(brickCount);
        y.add(brickwoodCount);
        y.add(buildCount);
        y.add(factoryCount);
        y.add(highCount);
        y.add(mulConcreteCount);
        y.add(stoneCount);
        y.add(wideCount);
        y.add(woodCount);
        y.add(otherCount);
        HashMap<String,Object> buildCountMap=new HashMap<>();
        buildCountMap.put("x",x);
        buildCountMap.put("y",y);

        return buildCountMap;
    }

    /**
     * 柱状图
     *
     * @param analysisQueryVo
     * @return
     */
    @Override
    public Map<String, Object> findAreaCountMap(AnalysisQueryVo analysisQueryVo) {
        //砖混结构
        Double brickCount=brickStructureService.area(analysisQueryVo);
        //砖木结构
        Double brickwoodCount=brickwoodStructureService.area(analysisQueryVo);
        //土木结构
        Double buildCount= buildingStructureService.area(analysisQueryVo);
        //工业厂房结构
        Double factoryCount = factoryStructureService.area(analysisQueryVo);
        //高层结构
        Double highCount = highStructureService.area(analysisQueryVo);
        //多层钢筋混凝土结构
        Double mulConcreteCount = mulConcreteStructureService.area(analysisQueryVo);
        //其它结构
        Double otherCount = otherStructureService.area(analysisQueryVo);
        //石结构
        Double stoneCount = stoneStructureService.area(analysisQueryVo);
        //空旷结构
        Double wideCount = wideStructureService.area(analysisQueryVo);
        //木结构
        Double woodCount = woodStructureService.area(analysisQueryVo);
        // X轴数据
        ArrayList<String> x = new ArrayList<>();
        x.add("砖混结构");
        x.add("砖木结构");
        x.add("土木结构");
        x.add("工业厂房结构");
        x.add("高层结构");
        x.add("多层钢筋混凝土结构");
        x.add("石结构");
        x.add("空旷结构");
        x.add("木结构");
        x.add("其它结构");
        // Y轴数据
        ArrayList<Double> y = new ArrayList<>();

        y.add(brickCount);
        y.add(brickwoodCount);
        y.add(buildCount);
        y.add(factoryCount);
        y.add(highCount);
        y.add(mulConcreteCount);
        y.add(stoneCount);
        y.add(wideCount);
        y.add(woodCount);
        y.add(otherCount);
        HashMap<String,Object> buildCountMap=new HashMap<>();
        buildCountMap.put("x",x);
        buildCountMap.put("y",y);
        return buildCountMap;
    }

    @Override
    public void exportForBuildingCount(StructureQueryVo structureQueryVo, HttpServletResponse response) {
        String status = structureQueryVo.getStatus();
        String titleStr = "";
        if(!org.springframework.util.StringUtils.isEmpty(status) && !"null".equals(status)){
            //判断审核状态
            if(Constant.DAI_SP.equalsIgnoreCase(status)){
                titleStr = "待审核";
            }else if(Constant.SP_NOT.equalsIgnoreCase(status)){
                titleStr = "审核不通过";
            }else if(Constant.SP_YES.equalsIgnoreCase(status)){
                titleStr = "审核通过";
            }
        }else{
            titleStr = "已提交";
        }
        //确定查询数据范围
        String userId = PlatformSessionUtils.getUserId();
        List<RoleEntity> roles = PlatformAPI.getOrgAPI().getUserAPI().getRoles(userId);
        List<DeptEntity> depts = PlatformAPI.getOrgAPI().getUserAPI().getDepts(userId);
        DeptEntity deptEntity = null;
        if(depts!=null && depts.size()>0){
            deptEntity = depts.get(0);
        }
        DeptEntity dept = null;
        if(deptEntity!=null){
            dept = PlatformAPI.getOrgAPI().getDeptAPI().getDept(deptEntity.getDeptId());
        }

        //是否为超级管理员
        boolean containtRole = ServerUtil.isContaintRole(roles, superCode);
        //是否为省级管理员
        boolean containtRolePro = ServerUtil.isContaintRole(roles, provinceCode);
        //fwStructureFormRepository
        //超级管理员查询全部
        List<ExportForBuildingVo> structureBuildings = new ArrayList<>();
        List<ExportForBuildingWithTaskVo> structureBuildingWithTask = new ArrayList<>();
        if(containtRole){
            structureBuildings = fwStructureFormQueryRepository.getAllData(structureQueryVo.getStatus());
            structureBuildingWithTask = fwStructureFormQueryRepository.getAllDataWithTask(structureQueryVo.getStatus());
        }else{
             //其他查询各自省的
            assert dept != null;
            structureBuildings = fwStructureFormQueryRepository.getAllDataByProvinceName(dept.getDeptName(),structureQueryVo.getStatus()) ;
            structureBuildingWithTask = fwStructureFormQueryRepository.getAllDataWithTaskByProvinceName(dept.getDeptName(),structureQueryVo.getStatus()) ;
        }
        List<ExportForBuildingVo> vos = buildCountData(structureBuildings);
        List<ExportForBuildingWithTaskVo> tvos = buildTaskCountData(structureBuildingWithTask,containtRolePro);
        defaultExportForCount(vos,tvos,titleStr,response);

    }

    private List<ExportForBuildingWithTaskVo> buildTaskCountData(List<ExportForBuildingWithTaskVo> structureBuildingWithTasks,boolean containtRolePro) {
        List<ExportForBuildingWithTaskVo> vos = new ArrayList<>();
        if(structureBuildingWithTasks!=null && structureBuildingWithTasks.size()>0){
            String provinceName = "";
            String taskName = "";
            int sumCount =0;
            ExportForBuildingWithTaskVo vo = new ExportForBuildingWithTaskVo();
            //砖混结构/砖石木结构总数
            int bcount = 0;
            //砖木结构
            int zcount = 0;
            //定义多层钢筋混凝土结构
            int ccount = 0;
            //高层结构
            int hcount = 0;
            //工业厂房结构
            int fcount = 0;
            //空旷结构
            int wcount = 0;
            //土木结构
            int wocount = 0;
            //木结构
            int woocount = 0;
            //石结构
            int stcount = 0;
            //其他结构
            int ocount = 0;
            //合计
            int scount = 0;
            for(ExportForBuildingWithTaskVo evo:structureBuildingWithTasks ){
                if(StringUtils.isBlank(provinceName)){
                    provinceName = evo.getBelongtoProvince();
                    taskName = evo.getTaskName();
                }
                if(StringUtils.isEmpty(taskName)){
                    taskName = evo.getTaskName();
                }
                if(!taskName.equalsIgnoreCase(evo.getTaskName())){
                    taskName = evo.getTaskName();
                    vo.setSumCount(sumCount);
                    scount+=sumCount;
                    sumCount = 0;
                    vos.add(vo);
                    vo = new ExportForBuildingWithTaskVo();
                }

                if(StringUtils.isNotBlank(provinceName) && !provinceName.equalsIgnoreCase(evo.getBelongtoProvince())&& !StringUtils.isEmpty(evo.getBelongtoProvince())){
                    provinceName = evo.getBelongtoProvince();
                }

                vo.setBelongtoProvince(evo.getBelongtoProvince());
                vo.setTaskName(taskName);
                if(Constant.BRICK_STRUCTURE.equals(evo.getStructurType())){
                    vo.setBrickCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    bcount+=(evo.getNumber().intValue());
                }else if(Constant.MUL_CONCRETE_STRUCTURE.equals(evo.getStructurType())){
                    vo.setMulConcreteCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    ccount+=(evo.getNumber().intValue());
                }else if(Constant.HIGH_STRUCTURE.equals(evo.getStructurType())){
                    vo.setHighCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    hcount+=(evo.getNumber().intValue());
                }else if(Constant.FACTORY_STRUCTURE.equals(evo.getStructurType())){
                    vo.setFactoryCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    fcount+=(evo.getNumber().intValue());
                }else if(Constant.WIDE_STRUCTURE.equals(evo.getStructurType())){
                    vo.setWideCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    wcount+=(evo.getNumber().intValue());
                }else if(Constant.BUILDING_STRUCTURE.equals(evo.getStructurType())){
                    vo.setBuildingCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    wocount+=(evo.getNumber().intValue());
                }else if(Constant.WOOD_STRUCTURE.equals(evo.getStructurType())){
                    vo.setWoodCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    wocount+=(evo.getNumber().intValue());
                }else if(Constant.STONE_STRUCTURE.equals(evo.getStructurType())){
                    vo.setStoneCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    stcount+=(evo.getNumber().intValue());
                }else if(Constant.OTHER_STRUCTURE.equals(evo.getStructurType())){
                    vo.setOtherCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    ocount+=(evo.getNumber().intValue());
                }else if(Constant.BRICKWOOD_STRUCTURE.equals(evo.getStructurType())){
                    vo.setBrickwoodCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    zcount+=(evo.getNumber().intValue());
                }
            }
            //if(containtRolePro){
                vo.setSumCount(sumCount);
                scount+=sumCount;
                vos.add(vo);
                vo = new ExportForBuildingWithTaskVo();
            //}
            vo.setBelongtoProvince("总计");
            vo.setTaskName("各个任务");
            vo.setBrickCount(new BigInteger(String.valueOf(bcount)));
            vo.setMulConcreteCount(new BigInteger(String.valueOf(ccount)));
            vo.setHighCount(new BigInteger(String.valueOf(hcount)));
            vo.setFactoryCount(new BigInteger(String.valueOf(fcount)));
            vo.setWideCount(new BigInteger(String.valueOf(wcount)));
            vo.setBuildingCount(new BigInteger(String.valueOf(wocount)));
            vo.setWoodCount(new BigInteger(String.valueOf(woocount)));
            vo.setStoneCount(new BigInteger(String.valueOf(stcount)));
            vo.setOtherCount(new BigInteger(String.valueOf(ocount)));
            vo.setBrickwoodCount(new BigInteger(String.valueOf(zcount)));
            vo.setSumCount(scount);
            vos.add(vo);
        }
        return vos;
    }

    private static List<ExportForBuildingVo> buildCountData(List<ExportForBuildingVo> structureBuildings){
        List<ExportForBuildingVo> vos = new ArrayList<>();

        if(structureBuildings!=null && structureBuildings.size()>0){
            String provinceName = "";
            int sumCount =0;
            ExportForBuildingVo vo = new ExportForBuildingVo();
            int count = 0;

            //砖混结构总数
            int bcount = 0;
            //砖木结构
            int zcount = 0;
            //定义多层钢筋混凝土结构
            int ccount = 0;
            //高层结构
            int hcount = 0;
            //工业厂房结构
            int fcount = 0;
            //空旷结构
            int wcount = 0;
            //土木结构
            int wocount = 0;
            //木结构
            int woocount = 0;
            //石结构
            int stcount = 0;
            //其他结构
            int ocount = 0;
            //合计
            int scount = 0;
            for(ExportForBuildingVo evo:structureBuildings ){
                if(StringUtils.isBlank(provinceName)){
                    provinceName = evo.getBelongtoProvince();
                }
                if(StringUtils.isNotBlank(provinceName) && !provinceName.equalsIgnoreCase(evo.getBelongtoProvince()) && !StringUtils.isEmpty(evo.getBelongtoProvince())){
                    provinceName = evo.getBelongtoProvince();
                    scount+=sumCount;
                    vo.setSumCount(sumCount);
                    sumCount = 0;
                    vos.add(vo);
                    vo = new ExportForBuildingVo();
                }
                vo.setBelongtoProvince(evo.getBelongtoProvince());
                vo.setStructurType(evo.getStructurType());
                if(Constant.BRICK_STRUCTURE.equals(evo.getStructurType())){
                    vo.setBrickCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    bcount+=(evo.getNumber().intValue());
                }else if(Constant.MUL_CONCRETE_STRUCTURE.equals(evo.getStructurType())){
                    vo.setMulConcreteCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    ccount+=(evo.getNumber().intValue());
                }else if(Constant.HIGH_STRUCTURE.equals(evo.getStructurType())){
                    vo.setHighCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    hcount+=(evo.getNumber().intValue());
                }else if(Constant.FACTORY_STRUCTURE.equals(evo.getStructurType())){
                    vo.setFactoryCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    fcount+=(evo.getNumber().intValue());
                }else if(Constant.WIDE_STRUCTURE.equals(evo.getStructurType())){
                    vo.setWideCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    wcount+=(evo.getNumber().intValue());
                }else if(Constant.BUILDING_STRUCTURE.equals(evo.getStructurType())){
                    vo.setBuildingCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    wocount+=(evo.getNumber().intValue());
                }else if(Constant.WOOD_STRUCTURE.equals(evo.getStructurType())){
                    vo.setWoodCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    woocount+=(evo.getNumber().intValue());
                }else if(Constant.STONE_STRUCTURE.equals(evo.getStructurType())){
                    vo.setStoneCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    stcount+=(evo.getNumber().intValue());
                }else if(Constant.OTHER_STRUCTURE.equals(evo.getStructurType())){
                    vo.setOtherCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    ocount+=(evo.getNumber().intValue());
                }else if(Constant.BRICKWOOD_STRUCTURE.equals(evo.getStructurType())){
                    vo.setBrickwoodCount(evo.getNumber());
                    sumCount+=(evo.getNumber().intValue());
                    zcount+=(evo.getNumber().intValue());
                }
                count++;
            }
            //处理省查询
            if(count == structureBuildings.size()){
                vo.setSumCount(sumCount);
                scount+=sumCount;
                vos.add(vo);
                vo = new ExportForBuildingVo();
            }
            vo.setBelongtoProvince("总计");
            vo.setBrickCount(new BigInteger(String.valueOf(bcount)));
            vo.setMulConcreteCount(new BigInteger(String.valueOf(ccount)));
            vo.setHighCount(new BigInteger(String.valueOf(hcount)));
            vo.setFactoryCount(new BigInteger(String.valueOf(fcount)));
            vo.setWideCount(new BigInteger(String.valueOf(wcount)));
            vo.setBuildingCount(new BigInteger(String.valueOf(wocount)));
            vo.setWoodCount(new BigInteger(String.valueOf(woocount)));
            vo.setStoneCount(new BigInteger(String.valueOf(stcount)));
            vo.setOtherCount(new BigInteger(String.valueOf(ocount)));
            vo.setBrickwoodCount(new BigInteger(String.valueOf(zcount)));
            vo.setSumCount(scount);
            vos.add(vo);
            //处理超级管理员查询没有走省切换保存集合的数据
//            if(count == structureBuildings.size() && vos.size()>0){
//                vo.setSumCount(sumCount);
//                vos.add(vo);
//            }
        }
        return vos;
    }
    private static void defaultExportForCount(List<ExportForBuildingVo> vos, List<ExportForBuildingWithTaskVo> tvos,String statusStr,HttpServletResponse response){
        // 导出
        //1.分别导出参数
        ExportParams buildParam = new ExportParams();
        ExportParams teskParam = new ExportParams();

        buildParam.setSheetName("按省统计祥查采集数据栋数");
        buildParam.setHeight((short) 18);
        buildParam.setStyle(ExcelStyleUtil.class);

        teskParam.setSheetName("按任务统计祥查采集数据栋数");
        teskParam.setHeight((short) 18);
        teskParam.setStyle(ExcelStyleUtil.class);

        //2.构建分别填充数据:data(数据)、title(标题)、entity(导出属性)
        Map<String, Object> exportBuildMap = new HashMap<String, Object>();
        Map<String, Object> exportTaskMap = new HashMap<String, Object>();

        exportBuildMap.put("data", vos);
        exportBuildMap.put("title", buildParam);
        exportBuildMap.put("entity", ExportForBuildingVo.class);

        exportTaskMap.put("data", tvos);
        exportTaskMap.put("title", teskParam);
        exportTaskMap.put("entity", ExportForBuildingWithTaskVo.class);

        //3.组装参数
        List<Map<String, Object>> sheetsList = new ArrayList<Map<String, Object>>();
        sheetsList.add(exportBuildMap);
        sheetsList.add(exportTaskMap);
        //EasyPoiExcelUtil.exportExcel(vos, "按省统计祥查采集数据栋数", "按省统计祥查采集数据栋数", ExportForBuildingVo.class, "按省统计祥查采集数据栋数.xls", response);
        EasyPoiExcelUtil.exportExcel(sheetsList, statusStr+"栋数统计.xls", response);
    }

    private static void defaultExportForArea(List<ExportForBuildingAreaVo> vos, List<ExportForBuildingAreaWithTaskVo> tvos,String statusStr ,HttpServletResponse response){
        // 导出
        //1.分别导出参数
        ExportParams buildParam = new ExportParams();
        ExportParams teskParam = new ExportParams();

        buildParam.setSheetName("按省统计祥查采集数据面积");
        buildParam.setHeight((short) 18);
        buildParam.setStyle(ExcelStyleUtil.class);
        teskParam.setHeaderHeight(18);

        teskParam.setSheetName("按任务统计祥查采集数据面积");
        teskParam.setHeight((short) 18);
        teskParam.setStyle(ExcelStyleUtil.class);
        teskParam.setHeaderHeight(18);

        //2.构建分别填充数据:data(数据)、title(标题)、entity(导出属性)
        Map<String, Object> exportBuildMap = new HashMap<String, Object>();
        Map<String, Object> exportTaskMap = new HashMap<String, Object>();

        exportBuildMap.put("data", vos);
        exportBuildMap.put("title", buildParam);
        exportBuildMap.put("entity", ExportForBuildingAreaVo.class);

        exportTaskMap.put("data", tvos);
        exportTaskMap.put("title", teskParam);
        exportTaskMap.put("entity", ExportForBuildingAreaWithTaskVo.class);

        //3.组装参数
        List<Map<String, Object>> sheetsList = new ArrayList<Map<String, Object>>();
        sheetsList.add(exportBuildMap);
        sheetsList.add(exportTaskMap);
        //EasyPoiExcelUtil.exportExcel(vos, "按省统计祥查采集数据栋数", "按省统计祥查采集数据栋数", ExportForBuildingVo.class, "按省统计祥查采集数据栋数.xls", response);
        EasyPoiExcelUtil.exportExcel(sheetsList, statusStr+"面积统计.xls", response);
    }

    @Override
    public void exportForBuildingArea(StructureQueryVo structureQueryVo, HttpServletResponse response) {
        String status = structureQueryVo.getStatus();
        String titleStr = "";
        if(!org.springframework.util.StringUtils.isEmpty(status) && !"null".equals(status)){
            //判断审核状态
            if(Constant.DAI_SP.equalsIgnoreCase(status)){
                titleStr = "待审核";
            }else if(Constant.SP_NOT.equalsIgnoreCase(status)){
                titleStr = "审核不通过";
            }else if(Constant.SP_YES.equalsIgnoreCase(status)){
                titleStr = "审核通过";
            }
        }else{
            titleStr = "已提交";
        }
        //确定查询数据范围
        String userId = PlatformSessionUtils.getUserId();
        List<RoleEntity> roles = PlatformAPI.getOrgAPI().getUserAPI().getRoles(userId);
        List<DeptEntity> depts = PlatformAPI.getOrgAPI().getUserAPI().getDepts(userId);
        DeptEntity deptEntity = null;
        if(depts!=null && depts.size()>0){
            deptEntity = depts.get(0);
        }
        DeptEntity dept = null;
        if(deptEntity!=null){
            dept = PlatformAPI.getOrgAPI().getDeptAPI().getDept(deptEntity.getDeptId());
        }

        //是否为超级管理员
        boolean containtRole = ServerUtil.isContaintRole(roles, superCode);
        //是否为省级管理员
        boolean containtRolePro = ServerUtil.isContaintRole(roles, provinceCode);
        //fwStructureFormRepository
        //超级管理员查询全部
        List<ExportForBuildingAreaVo> structureBuildings = new ArrayList<>();
        List<ExportForBuildingAreaWithTaskVo> structureBuildingWithTask = new ArrayList<>();
        if(containtRole){
            structureBuildings = fwStructureFormQueryRepository.getAllAreaData("",structureQueryVo.getStatus());
            structureBuildingWithTask = fwStructureFormQueryRepository.getAllAreaDataWithTask("",structureQueryVo.getStatus());
        }else{
            //其他查询各自省的
            assert dept != null;
            structureBuildings = fwStructureFormQueryRepository.getAllAreaData(dept.getDeptName(),structureQueryVo.getStatus()) ;
            structureBuildingWithTask = fwStructureFormQueryRepository.getAllAreaDataWithTask(dept.getDeptName(),structureQueryVo.getStatus()) ;
        }
        List<ExportForBuildingAreaVo> vos = buildAreaData(structureBuildings);
        List<ExportForBuildingAreaWithTaskVo> tvos = buildAreaWithTaskData(structureBuildingWithTask,containtRolePro);
        defaultExportForArea(vos,tvos,titleStr,response);

    }

    private List<ExportForBuildingAreaWithTaskVo> buildAreaWithTaskData(List<ExportForBuildingAreaWithTaskVo> structureBuildingWithTasks,Boolean containtRolePro) {
        List<ExportForBuildingAreaWithTaskVo> vos = new ArrayList<>();
        if(structureBuildingWithTasks!=null && structureBuildingWithTasks.size()>0){
            String provinceName = "";
            String taskName = "";
            BigDecimal sumCount = BigDecimal.ZERO;
            //砖混结构总数
            BigDecimal bcount = BigDecimal.ZERO;
            //砖木结构总数
            BigDecimal zcount = BigDecimal.ZERO;
            //定义多层钢筋混凝土结构
            BigDecimal ccount = BigDecimal.ZERO;
            //高层结构
            BigDecimal hcount = BigDecimal.ZERO;
            //工业厂房结构
            BigDecimal fcount = BigDecimal.ZERO;
            //空旷结构
            BigDecimal wcount = BigDecimal.ZERO;
            //土木结构
            BigDecimal wocount = BigDecimal.ZERO;
            //木结构
            BigDecimal woocount = BigDecimal.ZERO;
            //石结构
            BigDecimal stcount = BigDecimal.ZERO;
            //其他结构
            BigDecimal ocount = BigDecimal.ZERO;
            //合计
            BigDecimal scount = BigDecimal.ZERO;
            ExportForBuildingAreaWithTaskVo vo = new ExportForBuildingAreaWithTaskVo();
            for(ExportForBuildingAreaWithTaskVo evo:structureBuildingWithTasks ){
                if(StringUtils.isBlank(provinceName)){
                    provinceName = evo.getBelongtoProvince();
                    taskName = evo.getTaskName();
                }
                if(StringUtils.isEmpty(provinceName)){
                    provinceName = evo.getBelongtoProvince();
                }
                if(StringUtils.isEmpty(taskName)){
                    taskName = evo.getTaskName();
                }
                if(!taskName.equalsIgnoreCase(evo.getTaskName())){
                    taskName = evo.getTaskName();
                    vo.setSumCount(sumCount);
                    scount = scount.add(sumCount);
                    sumCount = BigDecimal.ZERO;
                    vos.add(vo);
                    vo = new ExportForBuildingAreaWithTaskVo();
                }
                if(StringUtils.isNotBlank(provinceName) && !provinceName.equalsIgnoreCase(evo.getBelongtoProvince())&& !StringUtils.isEmpty(evo.getBelongtoProvince())){
                    provinceName = evo.getBelongtoProvince();
                }

                vo.setBelongtoProvince(evo.getBelongtoProvince());
                vo.setTaskName(taskName);
                if(Constant.BRICK_STRUCTURE.equals(evo.getStructurType())){
                    vo.setBrickCount(evo.getNumber());
                    sumCount = sumCount.add(evo.getNumber());
                    bcount = bcount.add(evo.getNumber());
                }else if(Constant.MUL_CONCRETE_STRUCTURE.equals(evo.getStructurType())){
                    vo.setMulConcreteCount(evo.getNumber()==null?new BigDecimal("0.00"):evo.getNumber());
                    sumCount = sumCount.add(evo.getNumber()==null?new BigDecimal("0.00"):evo.getNumber());
                    ccount = ccount.add(evo.getNumber()==null?new BigDecimal("0.00"):evo.getNumber());
                }else if(Constant.HIGH_STRUCTURE.equals(evo.getStructurType())){
                    vo.setHighCount(evo.getNumber());
                    sumCount = sumCount.add(evo.getNumber());
                    hcount = hcount.add(evo.getNumber());
                }else if(Constant.FACTORY_STRUCTURE.equals(evo.getStructurType())){
                    vo.setFactoryCount(evo.getNumber());
                    sumCount = sumCount.add(evo.getNumber());
                    fcount = fcount.add(evo.getNumber());
                }else if(Constant.WIDE_STRUCTURE.equals(evo.getStructurType())){
                    vo.setWideCount(evo.getNumber());
                    sumCount = sumCount.add(evo.getNumber());
                    wcount = wcount.add(evo.getNumber());
                }else if(Constant.BUILDING_STRUCTURE.equals(evo.getStructurType())){
                    vo.setBuildingCount(evo.getNumber());
                    sumCount = sumCount.add(evo.getNumber());
                    wocount = wocount.add(evo.getNumber());
                }else if(Constant.WOOD_STRUCTURE.equals(evo.getStructurType())){
                    vo.setWoodCount(evo.getNumber());
                    sumCount =  sumCount.add(evo.getNumber());
                    woocount = woocount.add(evo.getNumber());
                }else if(Constant.STONE_STRUCTURE.equals(evo.getStructurType())){
                    vo.setStoneCount(evo.getNumber());
                    sumCount = sumCount.add(evo.getNumber());
                    stcount = stcount.add(evo.getNumber());
                }else if(Constant.OTHER_STRUCTURE.equals(evo.getStructurType())){
                    vo.setOtherCount(evo.getNumber());
                    sumCount =  sumCount.add(evo.getNumber());
                    ocount = ocount.add(evo.getNumber());
                }else if(Constant.BRICKWOOD_STRUCTURE.equals(evo.getStructurType())){
                    vo.setBrickwoodCount(evo.getNumber());
                    sumCount =  sumCount.add(evo.getNumber());
                    zcount = zcount.add(evo.getNumber());
                }
            }
            vo.setSumCount(sumCount);
            scount = scount.add(sumCount);
            vos.add(vo);
            vo = new ExportForBuildingAreaWithTaskVo();

            vo.setBelongtoProvince("总计");
            vo.setTaskName("各个任务");
            vo.setBrickCount(bcount);
            vo.setMulConcreteCount(ccount);
            vo.setHighCount(hcount);
            vo.setFactoryCount(fcount);
            vo.setWideCount(wcount);
            vo.setBuildingCount(wocount);
            vo.setWoodCount(woocount);
            vo.setStoneCount(stcount);
            vo.setOtherCount(ocount);
            vo.setBrickwoodCount(zcount);
            vo.setSumCount(scount==new BigDecimal("0")?new BigDecimal("0.00"):scount);
            vos.add(vo);
        }
        return vos;
    }

    private static List<ExportForBuildingAreaVo> buildAreaData(List<ExportForBuildingAreaVo> structureBuildings){
        List<ExportForBuildingAreaVo> vos = new ArrayList<>();
        if(structureBuildings!=null && structureBuildings.size()>0){
            String provinceName = "";
            BigDecimal sumCount = BigDecimal.ZERO;
            //砖混结构
            BigDecimal bcount = BigDecimal.ZERO;
            //砖木结构总数
            BigDecimal zcount = BigDecimal.ZERO;
            //定义多层钢筋混凝土结构
            BigDecimal ccount = BigDecimal.ZERO;
            //高层结构
            BigDecimal hcount = BigDecimal.ZERO;
            //工业厂房结构
            BigDecimal fcount = BigDecimal.ZERO;
            //空旷结构
            BigDecimal wcount = BigDecimal.ZERO;
            //土木结构
            BigDecimal wocount = BigDecimal.ZERO;
            //木结构
            BigDecimal woocount = BigDecimal.ZERO;
            //石结构
            BigDecimal stcount = BigDecimal.ZERO;
            //其他结构
            BigDecimal ocount = BigDecimal.ZERO;
            //合计
            BigDecimal scount = BigDecimal.ZERO;
            ExportForBuildingAreaVo vo = new ExportForBuildingAreaVo();
            int count = 0;
            for(ExportForBuildingAreaVo evo:structureBuildings ){
                if(StringUtils.isBlank(provinceName)){
                    provinceName = evo.getBelongtoProvince();
                }
                if(StringUtils.isNotBlank(provinceName) && !provinceName.equalsIgnoreCase(evo.getBelongtoProvince())&& !StringUtils.isEmpty(evo.getBelongtoProvince())){
                    provinceName = evo.getBelongtoProvince();
                    vo.setSumCount(sumCount);
                    scount = scount.add(sumCount);
                    sumCount = BigDecimal.ZERO;
                    vos.add(vo);
                    vo = new ExportForBuildingAreaVo();
                }

                vo.setBelongtoProvince(evo.getBelongtoProvince());
                vo.setStructurType(evo.getStructurType());
                if(Constant.BRICK_STRUCTURE.equals(evo.getStructurType())){
                    vo.setBrickCount(evo.getNumber());
                    sumCount = sumCount.add(evo.getNumber());
                    bcount = bcount.add(evo.getNumber());
                }else if(Constant.MUL_CONCRETE_STRUCTURE.equals(evo.getStructurType())){
                    vo.setMulConcreteCount(evo.getNumber());
                    sumCount = sumCount.add(evo.getNumber());
                    ccount = ccount.add(evo.getNumber());
                }else if(Constant.HIGH_STRUCTURE.equals(evo.getStructurType())){
                    vo.setHighCount(evo.getNumber());
                    sumCount = sumCount.add(evo.getNumber());
                    hcount = hcount.add(evo.getNumber());
                }else if(Constant.FACTORY_STRUCTURE.equals(evo.getStructurType())){
                    vo.setFactoryCount(evo.getNumber());
                    sumCount = sumCount.add(evo.getNumber());
                    fcount = fcount.add(evo.getNumber());
                }else if(Constant.WIDE_STRUCTURE.equals(evo.getStructurType())){
                    vo.setWideCount(evo.getNumber());
                    sumCount = sumCount.add(evo.getNumber());
                    wcount = wcount.add(evo.getNumber());
                }else if(Constant.BUILDING_STRUCTURE.equals(evo.getStructurType())){
                    vo.setBuildingCount(evo.getNumber());
                    sumCount = sumCount.add(evo.getNumber());
                    wocount = wocount.add(evo.getNumber());
                }else if(Constant.WOOD_STRUCTURE.equals(evo.getStructurType())){
                    vo.setWoodCount(evo.getNumber());
                    sumCount = sumCount.add(evo.getNumber());
                    woocount = woocount.add(evo.getNumber());
                }else if(Constant.STONE_STRUCTURE.equals(evo.getStructurType())){
                    vo.setStoneCount(evo.getNumber());
                    sumCount = sumCount.add(evo.getNumber());
                    stcount = stcount.add(evo.getNumber());
                }else if(Constant.OTHER_STRUCTURE.equals(evo.getStructurType())){
                    vo.setOtherCount(evo.getNumber());
                    sumCount = sumCount.add(evo.getNumber());
                    ocount = ocount.add(evo.getNumber());
                }else if(Constant.BRICKWOOD_STRUCTURE.equals(evo.getStructurType())){
                    vo.setBrickwoodCount(evo.getNumber());
                    sumCount = sumCount.add(evo.getNumber());
                    zcount = zcount.add(evo.getNumber());
                }
                count++;
            }
            //if(count == structureBuildings.size() && vos.size()==0){
                vo.setSumCount(sumCount);
                scount = scount.add(sumCount);
                vos.add(vo);
                vo = new ExportForBuildingAreaVo();

                vo.setBelongtoProvince("总计");
                vo.setBrickCount(bcount);
                vo.setMulConcreteCount(ccount);
                vo.setHighCount(hcount);
                vo.setFactoryCount(fcount);
                vo.setWideCount(wcount);
                vo.setBuildingCount(wocount);
                vo.setWoodCount(woocount);
                vo.setStoneCount(stcount);
                vo.setOtherCount(ocount);
                vo.setBrickwoodCount(zcount);
                vo.setSumCount(scount);
                vos.add(vo);
        }
        return vos;
    }

    @Override
    public JSONObject getCheckForQgSiftPage(StructureQueryVo structureQueryVo, int curPage, int pageSize) {
        Page<FwStructureFormEntity> structFormPage =fwStructureFormRepositoryQueryRepository.getCheckForQgSiftPage(structureQueryVo,curPage,pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(structFormPage);
        return jsonObject;
    }


    @Override
    @Transactional
    public void back(String id, CheckVo checkVo) {
        // 获取当前用户信息
        String userId = PlatformSessionUtils.getUserId();
        UserEntity user = PlatformAPI.getOrgAPI().getUserAPI().getUser(userId);
        FwStructureFormEntity structureFormEntity = findById(id);
        String userLevel = checkVo.getUserLevel();
        // 主表设置退回状态与原因
        structureFormEntity.setUpdateUser(user.getUserId());
        structureFormEntity.setUpdateTime(new Date());
        structureFormEntity.setStatus(checkVo.getStatus());
        //超级管理员
        if("5".equals(userLevel)){
            structureFormEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
        }else{
            structureFormEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
        }
        this.update(structureFormEntity);

        // 根据类型判断出具体子表，根据子表Id修改子表审核信息
        String structureId = structureFormEntity.getStructureId();
        String structureType = structureFormEntity.getStructureType();
        // 修改子表
        if (Constant.BRICK_STRUCTURE.equals(structureType)) {
            // 砖混结构子表
            FwBrickStructureEntity brickStructureEntity = brickStructureService.findById(structureId);
            brickStructureEntity.setStatus(checkVo.getStatus());
            brickStructureEntity.setUpdateUser(user.getUserId());
            brickStructureEntity.setUpdateTime(new Date());
            if("5".equals(userLevel)){
                brickStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
            }else{
                brickStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
            }
            brickStructureRepository.save(brickStructureEntity);
        }
        if (Constant.BUILDING_STRUCTURE.equals(structureType)) {
            // 土木结构子表
            FwBuildingStructureEntity buildingStructureEntity = buildingStructureService.findById(structureId);
            buildingStructureEntity.setStatus(checkVo.getStatus());
            buildingStructureEntity.setUpdateUser(user.getUserId());
            buildingStructureEntity.setUpdateTime(new Date());
            if("5".equals(userLevel)){
                buildingStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
            }else{
                buildingStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
            }
            buildingStructureRepository.save(buildingStructureEntity);
        }
        if (Constant.FACTORY_STRUCTURE.equals(structureType)) {
            // 工业厂房结构子表
            FwFactoryStructureEntity factoryStructureEntity = factoryStructureService.findById(structureId);
            factoryStructureEntity.setStatus(checkVo.getStatus());
            factoryStructureEntity.setUpdateUser(user.getUserId());
            factoryStructureEntity.setUpdateTime(new Date());
            if("5".equals(userLevel)){
                factoryStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
            }else{
                factoryStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
            }
            factoryStructureRepository.save(factoryStructureEntity);
        }
        if (Constant.HIGH_STRUCTURE.equals(structureType)) {
            // 高层结构
            FwHighStructureEntity highStructureEntity = highStructureService.findById(structureId);
            highStructureEntity.setStatus(checkVo.getStatus());
            highStructureEntity.setUpdateUser(user.getUserId());
            highStructureEntity.setUpdateTime(new Date());
            if("5".equals(userLevel)){
                highStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
            }else{
                highStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
            }
            highStructureRepository.save(highStructureEntity);
        }
        if (Constant.MUL_CONCRETE_STRUCTURE.equals(structureType)) {
            // 多层钢筋混凝土
            FwMulConcreteStructureEntity mulConcreteStructureEntity = mulConcreteStructureService.findById(structureId);
            mulConcreteStructureEntity.setStatus(checkVo.getStatus());
            mulConcreteStructureEntity.setUpdateUser(user.getUserId());
            mulConcreteStructureEntity.setUpdateTime(new Date());
            if("5".equals(userLevel)){
                mulConcreteStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
            }else{
                mulConcreteStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
            }
            mulConcreteStructureRepository.save(mulConcreteStructureEntity);
        }
        if (Constant.STONE_STRUCTURE.equals(structureType)) {
            // 石结构
            FwStoneStructureEntity stoneStructureEntity = stoneStructureService.findById(structureId);
            stoneStructureEntity.setStatus(checkVo.getStatus());
            stoneStructureEntity.setUpdateUser(user.getUserId());
            stoneStructureEntity.setUpdateTime(new Date());
            if("5".equals(userLevel)){
                stoneStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
            }else{
                stoneStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
            }
            stoneStructureRepository.save(stoneStructureEntity);
        }
        if (Constant.WIDE_STRUCTURE.equals(structureType)) {
            // 空旷结构
            FwWideStructureEntity wideStructureEntity = wideStructureService.findById(structureId);
            wideStructureEntity.setStatus(checkVo.getStatus());
            wideStructureEntity.setUpdateUser(user.getUserId());
            wideStructureEntity.setUpdateTime(new Date());
            if("5".equals(userLevel)){
                wideStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
            }else{
                wideStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
            }
            wideStructureRepository.save(wideStructureEntity);
        }
        if (Constant.WOOD_STRUCTURE.equals(structureType)) {
            // 木结构
            FwWoodStructureEntity woodStructureEntity = woodStructureService.findById(structureId);
            woodStructureEntity.setStatus(checkVo.getStatus());
            woodStructureEntity.setUpdateUser(user.getUserId());
            woodStructureEntity.setUpdateTime(new Date());
            if("5".equals(userLevel)){
                woodStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
            }else{
                woodStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
            }
            woodStructureRepository.save(woodStructureEntity);
        }
        if (Constant.OTHER_STRUCTURE.equals(structureType)) {
            // 其它结构
            FwOtherStructureEntity otherStructureEntity = otherStructureService.findById(structureId);
            otherStructureEntity.setStatus(checkVo.getStatus());
            otherStructureEntity.setUpdateUser(user.getUserId());
            otherStructureEntity.setUpdateTime(new Date());
            if("5".equals(userLevel)){
                otherStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
            }else{
                otherStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
            }
            otherStructureRepository.save(otherStructureEntity);
        }
    }

    @Override
    @Transactional
    public void batchBack(CheckVo checkVo) {
        // 获取当前用户信息
        String userId = PlatformSessionUtils.getUserId();
        UserEntity user = PlatformAPI.getOrgAPI().getUserAPI().getUser(userId);
        String userLevel = checkVo.getUserLevel();
        String[] ids = checkVo.getId().split(",");
        if(ids !=null && ids.length > 0){
            for(String id : ids) {
                FwStructureFormEntity structureFormEntity = findById(id);
                // 主表设置退回状态与原因
                structureFormEntity.setStatus(checkVo.getStatus());
                structureFormEntity.setUpdateUser(user.getUserId());
                structureFormEntity.setUpdateTime(new Date());
                //超级管理员
                if("5".equals(userLevel)){
                    structureFormEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
                }else{
                    structureFormEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
                }
                this.update(structureFormEntity);

                // 根据类型判断出具体子表，根据子表Id修改子表审核信息
                String structureId = structureFormEntity.getStructureId();
                String structureType = structureFormEntity.getStructureType();
                // 修改子表
                if (Constant.BRICK_STRUCTURE.equals(structureType)) {
                    // 砖混结构子表
                    FwBrickStructureEntity brickStructureEntity = brickStructureService.findById(structureId);
                    brickStructureEntity.setStatus(checkVo.getStatus());
                    brickStructureEntity.setUpdateUser(user.getUserId());
                    brickStructureEntity.setUpdateTime(new Date());
                    if("5".equals(userLevel)){
                        brickStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
                    }else{
                        brickStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
                    }
                    brickStructureRepository.save(brickStructureEntity);
                }
                if (Constant.BUILDING_STRUCTURE.equals(structureType)) {
                    // 土木结构子表
                    FwBuildingStructureEntity buildingStructureEntity = buildingStructureService.findById(structureId);
                    buildingStructureEntity.setStatus(checkVo.getStatus());
                    buildingStructureEntity.setUpdateUser(user.getUserId());
                    buildingStructureEntity.setUpdateTime(new Date());
                    if("5".equals(userLevel)){
                        buildingStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
                    }else{
                        buildingStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
                    }
                    buildingStructureRepository.save(buildingStructureEntity);
                }
                if (Constant.FACTORY_STRUCTURE.equals(structureType)) {
                    // 工业厂房结构子表
                    FwFactoryStructureEntity factoryStructureEntity = factoryStructureService.findById(structureId);
                    factoryStructureEntity.setStatus(checkVo.getStatus());
                    factoryStructureEntity.setUpdateUser(user.getUserId());
                    factoryStructureEntity.setUpdateTime(new Date());
                    if("5".equals(userLevel)){
                        factoryStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
                    }else{
                        factoryStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
                    }
                    factoryStructureRepository.save(factoryStructureEntity);
                }
                if (Constant.HIGH_STRUCTURE.equals(structureType)) {
                    // 高层结构
                    FwHighStructureEntity highStructureEntity = highStructureService.findById(structureId);
                    highStructureEntity.setStatus(checkVo.getStatus());
                    highStructureEntity.setUpdateUser(user.getUserId());
                    highStructureEntity.setUpdateTime(new Date());
                    if("5".equals(userLevel)){
                        highStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
                    }else{
                        highStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
                    }
                    highStructureRepository.save(highStructureEntity);
                }
                if (Constant.MUL_CONCRETE_STRUCTURE.equals(structureType)) {
                    // 多层钢筋混凝土
                    FwMulConcreteStructureEntity mulConcreteStructureEntity = mulConcreteStructureService.findById(structureId);
                    mulConcreteStructureEntity.setStatus(checkVo.getStatus());
                    mulConcreteStructureEntity.setUpdateUser(user.getUserId());
                    mulConcreteStructureEntity.setUpdateTime(new Date());
                    if("5".equals(userLevel)){
                        mulConcreteStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
                    }else{
                        mulConcreteStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
                    }
                    mulConcreteStructureRepository.save(mulConcreteStructureEntity);
                }
                if (Constant.STONE_STRUCTURE.equals(structureType)) {
                    // 石结构
                    FwStoneStructureEntity stoneStructureEntity = stoneStructureService.findById(structureId);
                    stoneStructureEntity.setStatus(checkVo.getStatus());
                    stoneStructureEntity.setUpdateUser(user.getUserId());
                    stoneStructureEntity.setUpdateTime(new Date());
                    if("5".equals(userLevel)){
                        stoneStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
                    }else{
                        stoneStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
                    }
                    stoneStructureRepository.save(stoneStructureEntity);
                }
                if (Constant.WIDE_STRUCTURE.equals(structureType)) {
                    // 空旷结构
                    FwWideStructureEntity wideStructureEntity = wideStructureService.findById(structureId);
                    wideStructureEntity.setStatus(checkVo.getStatus());
                    wideStructureEntity.setUpdateUser(user.getUserId());
                    wideStructureEntity.setUpdateTime(new Date());
                    if("5".equals(userLevel)){
                        wideStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
                    }else{
                        wideStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
                    }
                    wideStructureRepository.save(wideStructureEntity);
                }
                if (Constant.WOOD_STRUCTURE.equals(structureType)) {
                    // 木结构
                    FwWoodStructureEntity woodStructureEntity = woodStructureService.findById(structureId);
                    woodStructureEntity.setStatus(checkVo.getStatus());
                    woodStructureEntity.setUpdateUser(user.getUserId());
                    woodStructureEntity.setUpdateTime(new Date());
                    if("5".equals(userLevel)){
                        woodStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
                    }else{
                        woodStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
                    }
                    woodStructureRepository.save(woodStructureEntity);
                }
                if (Constant.OTHER_STRUCTURE.equals(structureType)) {
                    // 其它结构
                    FwOtherStructureEntity otherStructureEntity = otherStructureService.findById(structureId);
                    otherStructureEntity.setStatus(checkVo.getStatus());
                    otherStructureEntity.setUpdateUser(user.getUserId());
                    otherStructureEntity.setUpdateTime(new Date());
                    if("5".equals(userLevel)){
                        otherStructureEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
                    }else{
                        otherStructureEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
                    }
                    otherStructureRepository.save(otherStructureEntity);
                }
            }
        }

    }

    @Override
    public List<FwStructureFormEntity> getFormByTaskId(String id) {
        // 查询条件
        Specification querySpecification = new Specification<FwStructureFormEntity>() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root<FwStructureFormEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>();
                if (StringUtils.isNotBlank(id)) {
                        predicates.add(criteriaBuilder.equal(root.get("taskId"), id));
                }
                // 有效标识，即为有效的
                predicates.add(criteriaBuilder.equal(root.get("delFlag"), Constant.DEL_NO));
                return criteriaBuilder.and(predicates.toArray(new javax.persistence.criteria.Predicate[predicates.size()]));
            }
        };
        List<FwStructureFormEntity> all = fwStructureFormRepository.findAll(querySpecification);
        return all;
    }

    @Override
    public JSONObject checkForExpertSift(StructureQueryVo structureQueryVo, int curPage, int pageSize) {
        Page<FwStructureFormEntity> structFormPage =fwStructureFormRepositoryQueryRepository.checkForExpertSift(structureQueryVo,curPage,pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(structFormPage);
        return jsonObject;
    }

    @Override
    public Boolean expertIsEmpty(StructureQueryVo structureQueryVo) {
        List<String> list = this.getExpertIdsByStructureType(structureQueryVo);
        if (list == null || list.size() <= 0) {
            return true;
        }
        return false;
    }

    @Override
    public void expertExport(StructureQueryVo structureQueryVo, HttpServletResponse response) {
        List<String> list = this.getExpertIdsByStructureType(structureQueryVo);
        if (Constant.BRICK_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            brickStructureService.export(list, response);
        }
        if (Constant.MUL_CONCRETE_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            mulConcreteStructureService.export(list, response);
        }
        if (Constant.HIGH_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            highStructureService.export(list, response);
        }
        if (Constant.FACTORY_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            factoryStructureService.export(list, response);
        }
        if (Constant.WIDE_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            wideStructureService.export(list, response);
        }
        if (Constant.BUILDING_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            buildingStructureService.export(list, response);
        }
        if (Constant.WOOD_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            woodStructureService.export(list, response);
        }
        if (Constant.STONE_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            stoneStructureService.export(list, response);
        }
        if (Constant.OTHER_STRUCTURE.equals(structureQueryVo.getStructureType())) {
            otherStructureService.export(list, response);
        }
    }

    private List<String> getExpertIdsByStructureType(StructureQueryVo structureQueryVo) {
        List<String> ids =fwStructureFormRepositoryQueryRepository.getExpertIds(structureQueryVo);
        return ids;
    }

    @Override
    public Map<String,Integer> getBelongtoCount(String province, String city,String area) {
        Map<String,Integer> count = fwStructureFormRepositoryQueryRepository.getBelongtoProvince(province,city,area);
        return count;
    }
}
